package com.xuhao.examai.question.application.services.impl;

import com.xuhao.examai.question.application.services.QuestionService;
import com.xuhao.examai.question.domain.models.Question;
import com.xuhao.examai.question.domain.repositories.QuestionRepository;
import com.xuhao.examai.common.PageResult;
import com.xuhao.examai.exception.BusinessException;
import com.xuhao.examai.exception.NotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
public class QuestionServiceImpl implements QuestionService {

	private final QuestionRepository questionRepository;

	@Override
	public Question create(Question question) {
		log.info("创建题目: {}", question.getContent());

		if (!question.isValid()) {
			throw new BusinessException("题目数据无效");
		}

		return questionRepository.save(question);
	}

	@Override
	public Question update(Question question) {
		log.info("更新题目: {}", question.getId());

		if (!questionRepository.existsById(question.getId())) {
			throw new NotFoundException("题目", question.getId().toString());
		}

		if (!question.isValid()) {
			throw new BusinessException("题目数据无效");
		}

		return questionRepository.save(question);
	}

	@Override
	public void delete(Long id) {
		log.info("删除题目: {}", id);

		if (!questionRepository.existsById(id)) {
			throw new NotFoundException("题目", id.toString());
		}

		questionRepository.deleteById(id);
	}

	@Override
	@Transactional(readOnly = true)
	public Question get(Long id) {
		log.info("获取题目: {}", id);

		return questionRepository.findById(id)
				.orElseThrow(() -> new NotFoundException("题目", id.toString()));
	}

	@Override
	@Transactional(readOnly = true)
	public List<Question> listByExamId(Long examId) {
		log.info("获取考试题目列表: {}", examId);
		return questionRepository.findByExamId(examId);
	}

	@Override
	@Transactional(readOnly = true)
	public PageResult<Question> listByPage(Integer current, Integer size, String keyword, String questionType,
			String difficulty) {
		log.info("分页查询题目: current={}, size={}, keyword={}, type={}, difficulty={}",
				current, size, keyword, questionType, difficulty);

		// TODO: 实现分页查询逻辑
		// 这里需要根据实际的分页实现来完善
		List<Question> questions = questionRepository.findByQuestionType(questionType);
		return PageResult.of(questions, (long) questions.size(), current, size);
	}

	@Override
	@Transactional(readOnly = true)
	public List<Question> listByKnowledgePoint(String knowledgePoint) {
		log.info("根据知识点查询题目: {}", knowledgePoint);
		return questionRepository.findByKnowledgePoint(knowledgePoint);
	}

	@Override
	@Transactional(readOnly = true)
	public List<Question> listByDifficulty(String difficulty) {
		log.info("根据难度查询题目: {}", difficulty);
		return questionRepository.findByDifficulty(difficulty);
	}

	@Override
	@Transactional(readOnly = true)
	public List<Question> listByQuestionType(String questionType) {
		log.info("根据类型查询题目: {}", questionType);
		return questionRepository.findByQuestionType(questionType);
	}

	@Override
	public List<Question> batchCreate(List<Question> questions) {
		log.info("批量创建题目: {}", questions.size());

		for (Question question : questions) {
			if (!question.isValid()) {
				throw new BusinessException("题目数据无效: " + question.getContent());
			}
		}

		return questions.stream()
				.map(questionRepository::save)
				.toList();
	}

	@Override
	@Transactional(readOnly = true)
	public boolean validateAnswer(Long questionId, String answer) {
		log.info("验证题目答案: questionId={}", questionId);

		Question question = get(questionId);
		return question.isCorrectAnswer(answer);
	}
}